package edu.hunau.cxb19.oop.class15.ex;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-16 15:13:07
 */
public class CheckExceptionTest {

	@Test
	public void test() throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			dateFormat.parse("2019/01-02");
		} catch (ParseException e) {
			System.out.println("[记录日志]:出现解析异常....");
			throw e;
		} catch (Exception e) {
			System.out.println("[记录日志]:出现异常....");
			throw e;
		}
	}

	public void test01() {
		int[] arr = new int[5];
		try {
			arr[6] = 10;
		} catch (NullPointerException e) {
			throw e;
		} catch (ArrayIndexOutOfBoundsException e) {
			throw e;
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public void test02() {
		int[] arr = new int[5];
		try {
			arr[6] = 10;
			// 从jdk7.0以后提供以下语法，简化catch过多造成程序可读性低的问题
			// 必须是同一个等级的异常才能使用该语法
		} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {

			throw e;
		} catch (RuntimeException e) {
			throw e;
		}
	}

	@Test
	public void test03() throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			dateFormat.parse("2019/01-02");

		} catch (Exception e) {
			System.out.println("日志记录:出现异常");
			throw e;
		} finally {
			System.out.println("finally...");
		}
	}

	@Test
	public void exTest() {
		System.out.println(ex());
	}

	public int ex() {
		try {
			if (2 > 1) {
				throw new RuntimeException();
			}
			return 1;
		} catch (RuntimeException e) {
			return 2;
		} finally {
			return 3;
		}

	}
}
